﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Web;
using System.Text.RegularExpressions;
using System.IO;

namespace COMMON
{
    public class ControlCommon
    {
        //显示成功提示框
        public static void ShowMessageBoxOk(string strMessage)
        {
            MessageBox.Show(strMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //显示错误提示框
        public static void ShowMessageBoxError(string strMessage)
        {
            MessageBox.Show(strMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 绑定ComboList数据
        /// </summary>
        /// <param name="dtValue"></param>
        /// <param name="cb"></param>
        public static void ComboBinds(DataTable dtValue, ComboBox cb)
        {
            cb.Items.Clear();
            DataRow dr = dtValue.NewRow();
            dr["LName"] = "--请选择--";
            dr["LValue"] = "-999";
            dtValue.Rows.InsertAt(dr, 0);
            cb.DisplayMember = "LName";
            cb.ValueMember = "LValue";
            cb.DataSource = dtValue;
            cb.SelectedIndex = 0;
        }

        /// <summary>
        /// 转换dt 到　ComboList需要类型
        /// </summary>
        /// <param name="DtValue">dt</param>
        /// <param name="NameField">Combolist　NameKey</param>
        /// <param name="ValueField">Combolist　ValueKey</param>
        /// <returns></returns>
        public static DataTable ConvertToComboList(DataTable DtValue, string NameField, string ValueField)
        {
            DataTable dtNew = new DataTable();
            dtNew.Columns.Add("LName");
            dtNew.Columns.Add("LValue");
            foreach (DataRow dr in DtValue.Rows)
            {
                DataRow row = dtNew.NewRow();
                row["LName"] = dr[NameField].ToString();
                row["LValue"] = dr[ValueField].ToString();
                dtNew.Rows.Add(row);
            }
            return dtNew;
        }


        /// <summary>
        /// send mail content when user register successful
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string FileMsg(string FilePath)
        {
            string strFile = "";
            if (File.Exists(FilePath))
            {
                StreamReader fr = new StreamReader(FilePath, System.Text.Encoding.Default);
                strFile = fr.ReadToEnd();
                fr.Close();
            }
            return strFile;
        }

        /// <summary>
        /// 磁盘Log
        /// </summary>
        /// <param name="me"></param>
        public static void DiskLog(string fileName, string ex)
        {
            StreamWriter writer = null;
            try
            {
                string strPath = System.AppDomain.CurrentDomain.BaseDirectory + "Log";
                if (System.IO.Directory.Exists(strPath) == false)
                {
                    System.IO.Directory.CreateDirectory(strPath);
                }

                writer = new StreamWriter(strPath + "\\" + fileName + "_" + DateTime.Today.ToString("yyyy-MM-dd") + ".log", true);
                writer.WriteLine("<----- " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ----->");
                writer.WriteLine(" 描述：" + ex);

            }
            catch { }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }
            }
        }


        /// <summary>
        /// 删除内容中的HTML标签
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string RemoveHtmlTag(string content)
        {
            MatchCollection mcStart = Regex.Matches(content, @"<[^/][^>]*>", RegexOptions.IgnoreCase);
            MatchCollection mcEnd = Regex.Matches(content, @"</[^>]+>", RegexOptions.IgnoreCase);
            foreach (Match m in mcStart)
            {
                content = content.Replace(m.Value, "");
            }
            foreach (Match m2 in mcEnd)
            {
                content = content.Replace(m2.Value, "");
            }
            return content;
        }

        /// <summary>
        /// 去html标签 截取字段
        /// </summary>
        /// <param name="Str">文章</param>
        /// <param name="sub">截取长度</param>
        /// <returns></returns>
        public static string SubToString(string Str, int sub)
        {
            string RemStr = RemoveHtmlTag(Str);

            return RemStr.Length > sub ? RemStr.Substring(0, sub - 1) : RemStr;
        }
        
        /// <summary>
        /// 截取字段
        /// </summary>
        /// <param name="str">string</param>
        /// <param name="len">int</param>
        /// <returns>string</returns>
        public static string CutStr(string str, int len)
        {
            if (!string.IsNullOrEmpty(str))
            {
                System.Text.RegularExpressions.Regex rChinese = new Regex(@"[\u4e00-\u9fa5]"); //验证中文
                Regex rEnglish = new Regex(@"^[A-Za-z0-9]+$");  //验证字母
                if (rChinese.IsMatch(str))
                {
                    //中文
                    return (str.Length > len) ? str.Substring(0, len) + "..." : str; ;
                }
                else if (rEnglish.IsMatch(str))
                {
                    //英文
                    return (str.Length > len * 2) ? str.Substring(0, len * 2) + "..." : str; ;
                }
                return (str.Length > len) ? str.Substring(0, len) + "..." : str; ;
            }
            return "";
        }



        /// <summary>
        /// 真返回true
        /// </summary>
        /// <param name="strNumber"></param>
        /// <returns></returns>
        public static bool IsNumber(string strNumber)
        {
            Regex regex = new Regex("[^0-9]");
            return !regex.IsMatch(strNumber);
        }

        /// <summary>
        /// 验证手机号码是否正确,成功为true,失败为false
        /// </summary>
        /// <param name="strNumber">string</param>
        /// <returns>bool</returns>
        public static bool IsExsitMebile(string strNumber)
        {
            Regex reg = new Regex(@"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\d{8}$");
            if(reg.IsMatch(strNumber))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 验证固定电话号码是否正确,成功为true,失败为false
        /// </summary>
        /// <param name="strNumber">string</param>
        /// <returns>bool</returns>
        public static bool IsExsitTelephone(string strNumber)
        {
            Regex reg = new Regex(@"0\d{2,3}-\d{5,9}|0\d{2,3}-\d{5,9}");
            if(reg.IsMatch(strNumber))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 验证邮箱地址格式是否正确,成功为true,失败为false
        /// </summary>
        /// <param name="strNumber">string</param>
        /// <returns>bool</returns>
        public static bool IsExsitEmail(string strNumber)
        {
            Regex reg=new Regex(@"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
            if (reg.IsMatch(strNumber))
            {
                return true;
            }
            return false;
        }
    }
}
